
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/rank/app_bar.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/rank/rank_view.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/rank/rank_req.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/utils/image_util.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

import 'actions.dart';

class RankListMainPage extends StatefulWidget {
  final String title;

  RankListMainPage({Key key, this.title}) : super(key: key);

  _RankListMainPageState createState() => _RankListMainPageState();
}

class _RankListMainPageState extends State<RankListMainPage>
    with SingleTickerProviderStateMixin {
  List<Tab> tabList() {
    return [
      Tab(
        text: '个人能力',
      ),
      Tab(
        text: '角色属性',
      ),
      Tab(
        text: '千机伞',
      ),
      Tab(
        text: '金币',
      ),
    ];
  }

  void _loadRankInfo(store) {
    var coinlReq = PageInfo(page: 1, pageSize: 100);
    var abilityReq = PageInfo(page: 1, pageSize: 100,type: "yexiu_capacity");
//    var abilityReq = PageInfo(page: 1, pageSize: 100,type: "culture");
    var weaponReq = PageInfo(page: 1, pageSize: 100,type: "weapon");
    var roleReq = PageInfo(page: 1, pageSize: 100,type: "role");
    //金币
    PluginEve.sendMessage(PluginConstant.httpYXGetCoinRanking,
            param: coinlReq, httpType: HttpType.POST)
        .then((data) {
      userCoinRank(store, data);
    });
    //武器 千机伞
    PluginEve.sendMessage(PluginConstant.httpYXGetRanking,
            param: weaponReq, httpType: HttpType.POST)
        .then((data) {
      yexiuRank(store, data);

      //角色
      PluginEve.sendMessage(PluginConstant.httpYXGetRanking,
          param: roleReq, httpType: HttpType.POST)
          .then((data) {
        yexiuRank(store, data);
      });
    });
    //能力
    PluginEve.sendMessage(PluginConstant.httpYXGetAbilityRanking,
            param: abilityReq, httpType: HttpType.POST)
        .then((data) {
      userAbilityRank(store, data);
    });
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(onInit: (store) {
      _loadRankInfo(store);
    }, converter: (store) {
      return AppStateViewModel(state: store.state);
    }, builder: (context, vm) {
      return Container(
          width: setRatio(375),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(imageUtilYeXiu('rank/icon_rank_main_bg@3x')),
              alignment: Alignment.topCenter,
              fit: BoxFit.fill,
            ),
          ),
          child: SafeArea(
            child: DefaultTabController(
                length: tabList().length,
                initialIndex: 0,
                child: Scaffold(
                  backgroundColor: Colors.transparent,
                  appBar: buildAppBar(widget.title, tabList()),
                  body: TabBarView(physics: BouncingScrollPhysics(), children: [
                    RankItem(vm.state.yeXiuState.rankState.abilityRankState),
                    RankItem(vm.state.yeXiuState.rankState.roleRankState),
                    RankItem(vm.state.yeXiuState.rankState.weaponRankState),
                    RankItem(vm.state.yeXiuState.rankState.coinRankState),
                  ]),
                )),
          ));
    });
  }
}
